﻿using UnityEngine;
using UnityEngine.EventSystems;

namespace HKTools
{
    /// <summary>
    /// HK_FirstPersonCam 角色子物体相机控制脚本
    /// </summary>
    public class HK_FirstPersonCamLook : HKFirstPersonCam_BaseController
    {
        // 定义常量替代
        const float LERP_SPEED = 16f;
        const float ZOOM_LERP_SPEED = 10f;

        [Header("相机旋转速度："), SerializeField] float mouseSensitivity = 280f;
        [Header("相机缩放最大值："), SerializeField] float maxFieldOfView = 80f;
        [Header("相机缩放最小值："), SerializeField] float minFieldOfView = 40f;
        [Header("相机缩放速度："), SerializeField] float zoomSpeed = 240f;

        bool isUIBlock;
        int mouseIndex = 0;

        Transform playerBody;
        Camera cam;

        float xEuler = 0f;
        float mouseX, mouseY;
        float mouseXVelocity, mouseYVelocity;
        float camFieldOfViewTarget;

        void Awake()
        {
            playerBody = transform.parent;

            if (playerBody == null)
            {
                HKDebug.LogError("HK_FirstPersonCamLook: 未找到父物体作为角色主体，请检查层级关系");
            }

            cam = GetComponent<Camera>();

            if (cam == null)
            {
                HKDebug.LogError("HK_FirstPersonCamLook: 未找到Camera组件，请检查挂载对象");
            }

            camFieldOfViewTarget = cam.fieldOfView;
        }

        void LateUpdate()
        {
            // UI遮挡检测
            if (isUIBlock && EventSystem.current?.IsPointerOverGameObject() == true)
                return;

            RotateCamera();
            ZoomCamera();
        }

        /// <summary>
        /// 按住鼠标左键/右键，旋转Camera和Player
        /// </summary>
        void RotateCamera()
        {
            if (Input.GetMouseButtonDown(mouseIndex))
            {
                Cursor.lockState = CursorLockMode.Locked;
            }

            if (Input.GetMouseButton(mouseIndex))
            {
                mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
                mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
            }
            else
            {
                mouseX = 0f;
                mouseY = 0f;
            }

            // 限制 mouseX 和 mouseY 的值，在-1f到1f之间
            mouseX = Mathf.Clamp(mouseX, -1f, 1f);
            mouseY = Mathf.Clamp(mouseY, -1f, 1f);

            if (Input.GetMouseButtonUp(mouseIndex))
            {
                Cursor.lockState = CursorLockMode.None;
            }

            Debug.Log("x: " + mouseX + " y: " + mouseY);

            mouseXVelocity = Mathf.Lerp(mouseXVelocity, mouseX, Time.deltaTime * LERP_SPEED);
            mouseYVelocity = Mathf.Lerp(mouseYVelocity, mouseY, Time.deltaTime * LERP_SPEED);

            xEuler -= mouseYVelocity;
            xEuler = Mathf.Clamp(xEuler, -80f, 80f);

            transform.localRotation = Quaternion.Euler(xEuler, 0, 0);
            playerBody?.Rotate(Vector3.up * mouseXVelocity);
        }

        /// <summary>
        /// 滚动鼠标中键缩放摄像机
        /// </summary>
        void ZoomCamera()
        {
            float scrollInput = Input.GetAxis("Mouse ScrollWheel");

            if (scrollInput != 0)
            {
                camFieldOfViewTarget -= scrollInput * zoomSpeed * Time.deltaTime;
                camFieldOfViewTarget = Mathf.Clamp(camFieldOfViewTarget, minFieldOfView, maxFieldOfView);
            }

            cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, camFieldOfViewTarget, Time.deltaTime * ZOOM_LERP_SPEED);
        }

        public void ResetRot()
        {
            xEuler = 0;
        }

        public void Init(bool isLeftMouseCtrl, bool isUIBlock)
        {
            if (isLeftMouseCtrl)
                mouseIndex = 0;
            else
                mouseIndex = 1;

            this.isUIBlock = isUIBlock;
        }
    }
}